package 代码随想录.回溯.a;

import java.util.ArrayList;
import java.util.List;

public class a17电话号码的字母组合 {
    static String[] tel = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    static StringBuilder cur;
    static List<String> res;

    static class Solution {
        public List<String> letterCombinations(String digits) {
            int index = 0;
            cur = new StringBuilder();
            res = new ArrayList<>();
            backtracking(digits, index);
            return res;
        }

        private void backtracking(String digits, int index) {
            if (index == digits.length()) {
                if (cur.length() != 0) {
                    res.add(cur.toString());
                }
                return;
            }
            char ch = digits.charAt(index);
            String telch = tel[ch - '0'];
            for (int i = 0; i < telch.length(); i++) {
                cur.append(telch.charAt(i));
                backtracking(digits, index + 1);
                cur.deleteCharAt(cur.length() - 1);
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String digits = "23";
        System.out.println(solution.letterCombinations(digits));
    }
}
